<?php


class Role extends CActiveRecord
{
	
	public function tableName()
	{
		return 'role';
	}

	
	public function rules()
	{
		
		
		return array(
			array('MemberGroupID, MenuAdminID, IsView, IsActive, IsPublish, IsAdd, IsDelete, IsEdit, IsExport, IsImport', 'numerical', 'integerOnly'=>true),
			
			
			array('RoleID, MemberGroupID, MenuAdminID, IsView, IsActive, IsPublish, IsAdd, IsDelete, IsEdit, IsExport, IsImport', 'safe', 'on'=>'search'),
		);
	}

	
	public function relations()
	{
		
		
		return array(
		);
	}

	
	public function attributeLabels()
	{
		return array(
			'RoleID' => 'Role',
			'MemberGroupID' => 'Nhóm',
			'MenuAdminID' => 'Tên menu',
			'IsView' => 'Xem',
			'IsActive' => 'Kích hoạt',
			'IsPublish' => 'Kiểm duyệt',
			'IsAdd' => 'Thêm',
			'IsDelete' => 'Xóa',
			'IsEdit' => 'Sửa',
			'IsExport' => 'Xuất bản',
			'IsImport' => 'Im port',
		);
	}

	
	public function search()
	{
		

		$V0v5vsnfxezw=new CDbCriteria;

		$V0v5vsnfxezw->compare('RoleID',$this->RoleID,true);
		$V0v5vsnfxezw->compare('MemberGroupID',$this->MemberGroupID);
		$V0v5vsnfxezw->compare('MenuAdminID',$this->MenuAdminID);
		$V0v5vsnfxezw->compare('IsView',$this->IsView);
		$V0v5vsnfxezw->compare('IsActive',$this->IsActive);
		$V0v5vsnfxezw->compare('IsPublish',$this->IsPublish);
		$V0v5vsnfxezw->compare('IsAdd',$this->IsAdd);
		$V0v5vsnfxezw->compare('IsDelete',$this->IsDelete);
		$V0v5vsnfxezw->compare('IsEdit',$this->IsEdit);
		$V0v5vsnfxezw->compare('IsExport',$this->IsExport);
		$V0v5vsnfxezw->compare('IsImport',$this->IsImport);

		return new CActiveDataProvider($this, array(
			'criteria'=>$V0v5vsnfxezw,
		));
	}

	
	public static function model($Vgmajjibziuy=__CLASS__)
	{
		return parent::model($Vgmajjibziuy);
	}
   
   
   public function getAll($Vxhwvpd5fwza = '',$Vlvls10sxhxu = array()){
      $Vxhwvpd5fwza = ($Vxhwvpd5fwza !='') ? ' LIMIT ' . $Vxhwvpd5fwza : '';
      $Vfvpskvzymzk = '';
      if(count($Vlvls10sxhxu) > 0){
         foreach($Vlvls10sxhxu as $V3onom3a4wpm=>$Vwe2w2kw1yhe){
            $Vfvpskvzymzk .= " AND " . $this->tableName() . "." . $V3onom3a4wpm . " =:" . $V3onom3a4wpm ; 
         }
      }
      $Vwmlpe2lzlsh =  "SELECT
               	role.MenuAdminID,
               	menuadmin.MenuAdminID,
               	membergroup.MemberGroupID as GroupID,
               	menuadmin.MenuName,
               	role.RoleID,
               	role.IsView,
               	role.IsActive,
               	role.IsPublish,
               	role.IsAdd,
               	role.IsDelete,
               	role.IsEdit,
               	membergroup.MembergroupName,
               	role.IsExport,
               	role.IsImport
               FROM
               	menuadmin
               INNER JOIN role ON menuadmin.MenuAdminID = role.MenuAdminID
               INNER JOIN membergroup ON membergroup.MemberGroupID = role.MemberGroupID
               WHERE 1 $Vfvpskvzymzk
               $Vxhwvpd5fwza";
      $Vjgnlv50f20c = Yii::app()->db;
      $Vyfkfneh1yzo = $Vjgnlv50f20c->createCommand($Vwmlpe2lzlsh);
      if(count($Vlvls10sxhxu) > 0){
         foreach($Vlvls10sxhxu as $V3onom3a4wpm=>$Vwe2w2kw1yhe){
            $Vyfkfneh1yzo->bindParam(":".$V3onom3a4wpm,$Vwe2w2kw1yhe);
         }
      }
      return $Vyfkfneh1yzo->queryAll();
      
   }
   
   
   public static function getRights($Vl5skhhltlbr){
      
      $V1k3uezktvnt = array('allow' => array('users' => array(),
                              'actions' => array()),
                     'deny' => array('users' => array(),
                              'actions' => array())
                     );
      $V5mmisnz4rvq = array(
         array('allow',
               'actions' => array('edit','index', 'view', 'create','delete','list','getListMenu','Active','Deactive','Loadmenu2','Loadmenu2Index','Loadmenu3','Loadmenu4'),
               'users' => array('chinh','admin'),
         ),
         array('deny',
               'users' => array('*'))
      );
      
      
      $V1k3uezktvnt = array();
      $Vwmlpe2lzlsh = "SELECT
     	         role.*
            FROM
            	role
            INNER JOIN menuadmin ON role.MenuAdminID = menuadmin.MenuAdminID
            WHERE MenuCode =:MenuCode";
      $Vjgnlv50f20c = Yii::app()->db;
      $Vyfkfneh1yzo = $Vjgnlv50f20c->createCommand($Vwmlpe2lzlsh);
      $Vyfkfneh1yzo->bindParam(":MenuCode",$Vl5skhhltlbr);
      $V02c3ztfj4ey = $Vyfkfneh1yzo->queryAll();
      $Vi3pe1cb1t20 = array();  
      $Virckwbeg044 = 0;
      foreach($V02c3ztfj4ey as $Virckwbeg044tem){
         
         $Vwmlpe2lzlsh = "SELECT
                  	member.LoginName
                  FROM
                  	member
                  WHERE 
                     MemberGroupID =".$Virckwbeg044tem['MemberGroupID'];
         $Vjgnlv50f20c = Yii::app()->db;
         $Vyfkfneh1yzo = $Vjgnlv50f20c->createCommand($Vwmlpe2lzlsh);
         
         $Vi3pe1cb1t20 = $Vyfkfneh1yzo->queryColumn(array("LoginName"));
         
         $V1k3uezktvnt[$Virckwbeg044][0] = 'deny';
         if($Virckwbeg044tem['IsView'] == 0){
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'index';
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'admin';
         }
         if($Virckwbeg044tem['IsAdd'] == 0){
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'create';
         }
         if($Virckwbeg044tem['IsEdit'] == 0){
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'edit';
         }
         if($Virckwbeg044tem['IsDelete'] == 0){
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'delete';
         }
         if($Virckwbeg044tem['IsActive'] == 0){
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'active';
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'deactive';
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'changeActive';
         }
         if($Virckwbeg044tem['IsPublish'] == 0){
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'publish';
         }
         if($Virckwbeg044tem['IsExport'] == 0){
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'export';
         }
         if($Virckwbeg044tem['IsImport'] == 0){
            $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'import';
         }
         $V1k3uezktvnt[$Virckwbeg044]['actions'][] = 'default'; 
         $V1k3uezktvnt[$Virckwbeg044]['users'] = (count($Vi3pe1cb1t20) > 0) ? $Vi3pe1cb1t20 : array('default'); 
         
         $Virckwbeg044++;
      }
      return $V1k3uezktvnt;
   }
   
}
